import {
  getListRoleApi,
  getTreeListApi,
  addRoleApi,
  delRoleApi,
  updateRoleApi,
  allocMenuApi,
  getListMenuApi,
  // listResourceApi,
  addResourceApi,
  listRoleResourceByRoleIdApi
  // updateResourceApi
} from '@/services/roles.js';
import { message } from 'ant-design-vue';
export default {
  namespaced: true,
  state: {
    roleListLoading: false,
    roleList: [],
    paginationOptions: {
      pageNum: 1,
      pageSize: 10,
      total: 0
    },
    addLoading: false,
    roleTreeList: [],
    roleTreeListO: [],
    roleTreeListP: [],
    userRoleList: [],
    listResourceData: [],
    listRoleResourceByRoleIdData: [],
    addResourceLoading: false,
    listRoleResourceLoading: false
  },
  mutations: {
    setState(state, { key, payload }) {
      state[key] = payload;
    }
  },
  getters: {
    menuRoles(state) {
      return state.roleTreeList;
    },
    menuRolesO(state) {
      return state.roleTreeListO;
    }
  },
  actions: {
    // 获取用户list
    async getListRole({ state, commit }, params) {
      commit('setState', { key: 'roleListLoading', payload: true });
      const { pageNum, pageSize } = state.paginationOptions;
      const apiParams = {
        pageNum,
        pageSize,
        ...params
      };
      const rep = await getListRoleApi(apiParams);
      commit('setState', { key: 'roleList', payload: rep.data.list });
      commit('setState', { key: 'paginationOptions', payload: { ...state.paginationOptions, total: rep.data.total }});
      commit('setState', { key: 'roleListLoading', payload: false });
    },
    // 新增
    async addRole({ commit }, params) {
      commit('setState', { key: 'addLoading', payload: true });
      try {
        const rep = await addRoleApi(params);
        if (rep.code === 200) {
          message.success(rep.message);
          return Promise.resolve(true);
        } else {
          message.error(rep.message);
        }
      } catch (error) {
        message.error('操作失败');
      } finally {
        commit('setState', { key: 'addLoading', payload: false });
      }
    },
    //  编辑
    async updateRole({ commit }, params) {
      commit('setState', { key: 'addLoading', payload: true });
      try {
        const rep = await updateRoleApi(params);
        if (rep.code === 200) {
          message.success(rep.message);
          return Promise.resolve(true);
        } else {
          message.error(rep.message);
        }
      } catch (error) {
        message.error('操作失败');
      } finally {
        commit('setState', { key: 'addLoading', payload: false });
      }
    },
    // 删除
    async delRole({ commit }, params) {
      try {
        const rep = await delRoleApi(params);
        if (rep.code === 200) {
          message.success(rep.message);
          return Promise.resolve(true);
        } else {
          message.error(rep.message);
        }
      } catch (error) {
        message.error('操作失败');
      }
    },
    // 删除
    async allocMenu({ commit }, params) {
      try {
        const rep = await allocMenuApi(params);
        if (rep.code === 200) {
          message.success(rep.message);
          return Promise.resolve(true);
        } else {
          message.error(rep.message);
        }
      } catch (error) {
        message.error('操作失败');
      }
    },
    async getListMenuApi({ commit }, params) {
      commit('setState', { key: 'userListMenuLoading', payload: true });

      const rep = await getListMenuApi(params);
      commit('setState', { key: 'userRoleList', payload: rep.data });
      commit('setState', { key: 'userListMenuLoading', payload: false });
    },
    async getMenuRoles({ state, commit }) {
      try {
        const rep = await getTreeListApi();
        if (rep.code === 200) {
          const flatM = (pl = state.roleTreeList, nd = [], np = []) => {
            for (const item of pl) {
              nd.push(item);
              if (item.children?.length > 0) {
                np.push(item.id);
                flatM(item.children, nd, np);
              }
            }
            return { nd, np };
          };
          const newData = flatM(rep.data);
          commit('setState', { key: 'roleTreeList', payload: newData.nd });
          commit('setState', { key: 'roleTreeListO', payload: rep.data });
          commit('setState', { key: 'roleTreeListP', payload: newData.np });
          return { rep: true, mro: rep.data, mr: newData };
        } else {
          return Promise.reject(false);
        }
      } catch (error) {
        return Promise.reject(false);
      }
    },
    // 获取资源
    async listResource({ state, commit }) {
      // const rep = await listResourceApi();
      const repFlist = [
        {
          name: '报文上行',
          code: 1000
        },
        {
          name: '文件上传',
          code: 1100
        },
        {
          name: '保存机场详情',
          code: 1200
        }
      ];

      // const newData = repFlist.filter(item => (rep.data ?? []).includes(item.code));
      commit('setState', { key: 'listResourceData', payload: repFlist });
    },
    // 回显
    async getListRoleResourceByRoleId({ commit }, params) {
      commit('setState', { key: 'listRoleResourceLoading', payload: true });

      const rep = await listRoleResourceByRoleIdApi(params);

      const newData = (rep.data ?? []).flatMap(item => item.resourceCode);
      commit('setState', { key: 'listRoleResourceByRoleIdData', payload: newData });
      commit('setState', { key: 'listRoleResourceLoading', payload: false });
    },
    //  新增
    async addResource({ commit }, params) {
      commit('setState', { key: 'addResourceLoading', payload: true });
      try {
        const rep = await addResourceApi(params);
        if (rep.code === 200) {
          message.success(rep.message);
          return Promise.resolve(true);
        } else {
          message.error(rep.message);
        }
      } catch (error) {
        message.error('操作失败');
      } finally {
        commit('setState', { key: 'addResourceLoading', payload: false });
      }
    }
  }
};
